<template>
    <div id="app">
        <fieldset>
            <legend>{{isLogin?'内容界面':'登录界面'}}</legend>
            <div class="content" v-if="isLogin">
                <h3>列表展示</h3>
                <button @click="isLogin=false">点击退出登录</button>
                <button @click="getList">点击获取列表数据</button>
                <h4>
                    <img :src="userInfo.headimgurl" width="50px" alt="">
                    昵称:<a href="">{{userInfo.nickname}}</a></h4>
                <ul>
                    <li v-for="item in lists" :key="item._id ">
                        <b>{{item.name}}</b>
                        <button v-has:read="permissionBtns">查看</button>
                        <button v-has:edit="permissionBtns">修改</button>
                        <button v-has:delete="permissionBtns">删除</button>
                        <button v-has:add="permissionBtns">增加</button>
                    </li>
                </ul>
            </div>
            <div class="login" v-else>
                <h3>请登录</h3>
                <label for="text">用户名</label>
                <input type="text" id="username" v-model="username">
                <br/>
                <label for="password">密码</label>
                <input type="text" v-model="password" @keydown.enter="login">
                <br/>
                <button @click="login">登入</button>
            </div>
            
        </fieldset>
    </div>
</template>

<script>
    const loginApi = (params = {}) => {
    return fetch('http://chst.vip/users/login', {
    //请求方式
    method: 'post',
    //请求头
    headers: {
    'content-type': 'application/json'
    },
    //携带的参数(上载的参数)
    body: JSON.stringify(params)
    })
    .then(body => body.json())
    .catch(err => err.toString())
    }
     const getlistApi = ()=>{
        return fetch('http://chst.vip/students/getstulist?count=10',{
            methohds:'get',
            headers:{
            'authorization':sessionStorage.getItem('user-token')
        }
     })
     .then(body=>body.json())
     .catch(err=>err.toString())
        }
       
    
    export default {
        directives:{
            has:{
                inserted(el,binding){
                    // el.disabled=true;    
                    let role = binding.arg;
                    let userRole = binding.value;
                    userRole.includes(role);
                    let hasRole = userRole.includes(role);
                    if(!hasRole){
                        el.disabled = true;
                        // el.parentNode.removeChild(el)

                    }
                }
            }
        },
        data(){
            return {
                isLogin:false,//是否登入状态
                username:"",
                password:"",
                lists:[],
                permissionBtns:[]
            }
        },
        methods:{
            async login() {
    //使用async和await来等待请求的异步结果
            let res = await loginApi({ username: this.username, password: this.password });
            console.log(res)

            if(res.state===true){
                this.isLogin = true;
                this.userInfo = res.userInfo;
                sessionStorage.setItem('user-token',res.token);
                this.permissionBtns=res.permission.buttons; 
                // this.getList()
            }else{
                alert(res.msg)
            }
            },
            async getList(){
               let res = await getlistApi()
               if(res.state){
                this.lists=res.data
               }else{
                alert(res.msg)
               }
               
            }

        }
    }
</script>

<style scoped>
    ul {
    padding: 0;
    margin: 0;
    list-style: none;
    }
    li{
        margin:10px;
        max-width: 500px;
    }

</style>